import java.io.IOException;
import java.util.concurrent.BlockingQueue;

import org.jsoup.*;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 
 * @author bill, mayuri
 * @version Winter 2014
 * 
 */
 

public class PageParser implements  Runnable {


    /**
     * Parser writes all URLs found while parsing the page here.
     */
    private BlockingQueue<String> myUrlRetrievalQueue;

    /**
     * Parser reads this queue to parse the PageContent.
     */
    private BlockingQueue<PageContent> myPagesParserQueue;

    /**
     * Parser writes to the analyzer queue
     */
    private  BlockingQueue<ParsedPageContent> myPagesAnalyzerQueue;



    private Document my_doc;
    private String my_txt; //should be List<String>
    private Elements my_links; //should be List<String>


    public PageParser(BlockingQueue<String> theUrlRetrievalQueue,
                         BlockingQueue<PageContent> thePagesParserQueue,
                         BlockingQueue<ParsedPageContent> thePagesAnalyzerQueue) {

        this.myUrlRetrievalQueue = theUrlRetrievalQueue;
        this.myPagesParserQueue = thePagesParserQueue;
        this.myPagesAnalyzerQueue = thePagesAnalyzerQueue;

    }

    @Override
    public void run() {
        {
            try{
                while (!myPagesParserQueue.isEmpty())
                {
                    ParsedPageContent parsedPageContent = parseContent(myPagesParserQueue.poll());

                    //add to analyzer queue
                    //TODO: we need to do this.
                    myPagesAnalyzerQueue.add(parsedPageContent);

                }
                Thread.sleep(1000);
            }
            catch(InterruptedException exception){
                
            }
        }
        
        
    }


    public void parse() throws InterruptedException
    {
        while (!myPagesParserQueue.isEmpty())
        {
            ParsedPageContent parsedPageContent = parseContent(myPagesParserQueue.poll());

            //add to analyzer queue
            //TODO: we need to do this.
            System.out.println(parsedPageContent);
            myPagesAnalyzerQueue.add(parsedPageContent);

        }
    }


    public ParsedPageContent parseContent(PageContent pageContent) {

        ParsedPageContent htmlContent = new ParsedPageContent(pageContent.getUrl());

        //Handling links
        Document doc = Jsoup.parse(pageContent.getContent());
        doc.setBaseUri(pageContent.getUrl());

        //set the page text content
        htmlContent.setTextContent(doc.text());

        Elements links = doc.select("a[href]");
        for (Element link : links)
        {
            String subUrl = link.attr("abs:href");

            /**
             * New URLs found in a web page (but not text documents),
             * should be stored for later retrieval.
             */
            if(!subUrl.endsWith(".txt")) {
                htmlContent.addSubURL(subUrl);
                myUrlRetrievalQueue.add(subUrl);
            }

        }

        return htmlContent;

    }


}

